home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / Shaders / TexturingAndModeling:AProceduralApproach / KMShaders / KMPlanetClouds.sl < prev    next >
Encoding:
Text File  |  1995-03-22  |  3.7 KB  |  114 lines

  1. /* modified by wave to KMPlanetClouds for name space protection... */
  2. /*
  3.  * planetclouds.sl - surface for a semi-opaque cloud layer to be put on
  4.  *                   an earth-like planetary model.
  5.  *
  6.  * DESCRIPTION:
  7.  *      When put on a sphere, sets the color & opacity of the sphere to
  8.  *   make it look like the clouds surrounding an Earth-like planet.
  9.  *      The shader works by creating a fractal turbulence function over
  10.  *   the surface, then modulating the opacity based on this function in
  11.  *   a way that looks like clouds on a planetary scale.
  12.  *
  13.  *
  14.  * PARAMETERS:
  15.  *    Ka, Kd - the usual meaning
  16.  *    distortionscale - controls the amount of texture distortion
  17.  *    omega,lambda,octaves - the fractal characteristics of the clouds
  18.  *    p4 - beats me
  19.  *    offset - controls the zero crossings (where the clouds disappear)
  20.  *
  21.  *
  22.  * HINTS:
  23.  *   1. The way this shader is typically used is to have two concentric
  24.  *      spheres represent a planet.  The inner one is colored like the
  25.  *      surface of a planet (perhaps using the "terran" shader), and the
  26.  *      outer one uses the "planetclouds" shader.
  27.  *   2. The best effects are achieved when the clouds not only occlude
  28.  *      the view of the planet, but also shadow it.  The way to do this
  29.  *      with the Blue Moon Renderer is to let the light cast shadows,
  30.  *      then declare the cloud sphere as follows:
  31.  *           AttributeBegin
  32.  *             Attribute "render" "casts_shadows" "shade"
  33.  *             Surface "planetclouds"
  34.  *             Sphere 1 -1 1 360
  35.  *           AttributeEnd
  36.  *   3. The default values for the shader assume that the planet is
  37.  *      represented by a unit sphere.  The texture space and/or parameters
  38.  *      to this shader will need to be altered if the size of your planet
  39.  *      is radically different.
  40.  *
  41.  *
  42.  * AUTHOR: Ken Musgrave
  43.  *    Conversion to Shading Language and other minor changes by Larry Gritz.
  44.  *
  45.  * REFERENCES:
  46.  *    _Texturing and Modeling: A Procedural Approach_, by David S. Ebert, ed.,
  47.  *    F. Kenton Musgrave, Darwyn Peachey, Ken Perlin, and Steven Worley.
  48.  *    Academic Press, 1994.  ISBN 0-12-228760-6.
  49.  *
  50.  * HISTORY:
  51.  *    ???? - original texture developed by Ken Musgrave.
  52.  *    Feb 1994 - Conversion to Shading Language by L. Gritz
  53.  *
  54.  * last modified 1 March 1994 by lg
  55.  */
  56.  
  57.  
  58.  
  59. #define TWOPI (2*PI)
  60.  
  61. /* Use signed Perlin noise */
  62. #define snoise(x) ((2*noise(x))-1)
  63. #define DNoise(p) (2*(point noise(p)) - point(1,1,1))
  64. #define VLNoise(Pt,scale) (snoise(DNoise(Pt)+(scale*Pt)))
  65. #define VERY_SMALL 0.001
  66.  
  67.  
  68.  
  69. surface
  70. KMPlanetClouds (float Ka = 0.5, Kd = 0.75;
  71.             float distortionscale = 1;
  72.             float omega = 0.7;
  73.             float lambda = 2;
  74.             float octaves = 9;
  75.             float offset = 0;)
  76. {
  77.   point Pdistortion;        /* "distortion" vector */
  78.   point PP;                 /* Point after distortion */
  79.   float l, o, a, i;         /* Loop control for fractal sum */
  80.   float result;             /* Fractal sum is stored here */
  81.  
  82.   /* Transform to texture coordinates */
  83.   PP = transform ("shader", P);
  84.  
  85.   /* Add in "distortion" vector */
  86.   Pdistortion = distortionscale * DNoise (PP);
  87.         /* Second cirrus: replace DNoise with vector fBm */
  88.   PP = PP + Pdistortion;
  89.  
  90.   /* Compute VLfBm */
  91.   l = 1;  o = 1;  a = 0;
  92.   for (i = 0;  i < octaves  &&  o >= VERY_SMALL;  i += 1) {
  93.       a += o * VLNoise (PP * l, 1);
  94.       l *= lambda;
  95.       o *= omega;
  96.     }
  97.   result = a;
  98.  
  99.   /* Adjust zero crossing (where the clouds disappear) */
  100.   result += offset;
  101.  
  102.   if (result < 0)
  103.       result = 0;
  104.  
  105.   /* Scale density */
  106.   result /= (1 + offset);
  107.  
  108.   /* Modulate surface opacity by the cloud value */
  109.   Oi = result * Os;
  110.  
  111.   /* Shade like matte, but with color scaled by cloud opacity */
  112.   Ci = Oi * (Ka * ambient() + Kd * diffuse(faceforward(normalize(N),I)));
  113. }
  114.